from flask import Flask, send_file,jsonify
from urllib import request
from scrapy.selector import Selector
import chardet

app = Flask(__name__)
url = "https://fubei.gitbooks.io/test/content/bian-geng-ji-lu.html"
req = request.Request(url)
req.add_header(
    'User-Agent', 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36')


@app.route('/')
def index():
    return send_file('templates/index.html')
 

@app.route('/get_items', methods=['GET'])
def get_items():
    return jsonify(parse(get_content(req)))


def parse(response):
    items = []
    for sel in response.css('div#book-search-results table tbody tr'):
        item = {}
        item['date'] = sel.css('td::text')[0].extract()
        item['author'] = sel.css('td::text')[1].extract()
        item['version'] = sel.css('td::text')[2].extract()
        item['decription'] = sel.css('td::text')[3].extract()
        items.append(item)
    return items


def get_content(req):
    with request.urlopen(req) as res:
        data = res.read()
        content = data.decode(chardet.detect(data).get('encoding'))
        return Selector(text=content, type="html")  # 将网页内容转换成Selector对象


if __name__ == '__main__':
    app.run()
